<template>
  <view>
    <my-search :radius=6 :bgcolor="'pink'" @click="gotoSearch"></my-search>
    <view class="scroll-view-container">
      <scroll-view class="left-scroll" scroll-y="true" :style="{height:windowHeight+'px'}">
        <block v-for="(item,i) in categoryList" :key="i">
          <view @click="handlerActive(i)" :class="['left-scroll-view-item', i === active?'active':'']">{{item.cat_name}}</view>
        </block>
      </scroll-view>
      <scroll-view class="right-scroll" scroll-y="true" :style="{height:windowHeight+'px'}" :scroll-top="scrollTop">
        <view class="cate-lv2" v-for="(item,i) in childCategoryList" :key="i">
          <view class="cate-lv2-title">
            {{item.cat_name}}
          </view>
          <view class="cate-lv3-list">
            <view class="cate-lv3-item" v-for="(item2,i2) in item.children" :key="i2" @click="jumpGoodsList(item2)">
              <image :src="item2.cat_icon" mode="widthFix"></image>
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins:[badgeMix],
    data() {
      return {
        windowHeight: 0,
        categoryList: [],
        active: 0,
        childCategoryList: [],
        scrollTop:0
      };
    },
    onLoad() {
      // 获取当前系统的信息
      const sysInfo = uni.getSystemInfoSync();
      // 为 wh 窗口可用高度动态赋值 -50 减去搜索框的高度 不然显示不全
      this.windowHeight = sysInfo.windowHeight -50;
      this.getCategoryList();
    },
    methods: {
      gotoSearch(){
        uni.navigateTo({
          url:'../../subpkg/search/search'
        })
      },
      handlerClick(){
        console.log('ok');
      },
      jumpGoodsList(item2){
        console.log('item2');
        console.log(item2);
        uni.navigateTo({
          url:'../../subpkg/goods-list/goods-list?cid='+item2.cat_id
        })
      },
      handlerActive(i) {
        this.active = i;
        this.childCategoryList = this.categoryList[i].children;
        this.scrollTop = this.scrollTop === 0 ? 1 : 0;
      },
      async getCategoryList() {
        // 发起请求
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        // 判断是否获取失败
        if (res.meta.status !== 200) return uni.$showMsg()
        // 转存数据
        this.categoryList = res.message
        this.childCategoryList = this.categoryList[this.active].children;
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scroll {
      width: 120px;
    }

    .left-scroll-view-item {
      line-height: 60px;
      text-align: center;
      font-size: 12px;
      background-color: #f7f7f7;

      //激活样式
      &.active {
        background-color: #ffffff;
        position: relative;

        &::before {
          content: ' ';
          position: absolute;
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }

    /* */
    .cate-lv2-title {
      font-size: 12px;
      font-weight: blod;
      text-align: center;
      padding: 15px 0;
    }
    .cate-lv3-list{
      display: flex;
      flex-wrap: wrap;/*如果一行撑不下 就自动换行*/
      .cate-lv3-item{
        width: 33.33%;
        display: flex;
        flex-direction: column;/*纵向布局*/
        justify-content: center;
        align-items: center;
        margin-bottom: 10px;
        image{
          width: 60px;
          height: 60px;
        }
        text{
          font-size: 12px;
        }
      }
    }
  }
</style>
